package pt.mauromad.guias.dao;

import static java.lang.String.format;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import pt.mauromad.guias.dao.generic.GenericAddressDao;

@Entity
@Table(name = "address")
public class AddressDao extends GenericAddressDao {
	private CompanyDao company;
	private String description;
	private boolean enabled = true;

	@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.EAGER, optional = false)
	@JoinColumn(name = "company_id", insertable = true, updatable = false, nullable = false)
	public CompanyDao getCompany() {
		return company;
	}

	public void setCompany(CompanyDao company) {
		this.company = company;
	}

	public boolean isEnabled() {
		return enabled;
	}

	@Column(nullable = false)
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Transient
	public String getName() {
		if (getDescription() == null)
			return company.getName();
		return format("%s - %s", company.getName(), getDescription());
	}

	@Transient
	public boolean isMainAddress() {
		return getId().equals(getCompany().getMainAddress().getId());
	}
}
